<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="收入编号" prop="incomeNo">
        <el-input
          v-model="queryParams.incomeNo"
          placeholder="请输入收入编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账户" prop="accountId">
        <el-select v-model="queryParams.accountId" placeholder="请选择账户" clearable>
          <el-option
            v-for="account in accountOptions"
            :key="account.accountId"
            :label="account.accountName"
            :value="account.accountId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="分类" prop="categoryId">
        <el-select v-model="queryParams.categoryId" placeholder="请选择分类" clearable>
          <el-option
            v-for="category in categoryOptions"
            :key="category.categoryId"
            :label="category.categoryName"
            :value="category.categoryId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option label="待确认" value="0" />
          <el-option label="已确认" value="1" />
          <el-option label="已取消" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="收入日期" prop="incomeDate">
        <el-date-picker
          v-model="queryParams.incomeDate"
          type="date"
          placeholder="选择收入日期"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:finance:income:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:finance:income:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:finance:income:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:finance:income:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="incomeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="收入编号" align="center" prop="incomeNo" />
      <el-table-column label="账户名称" align="center" prop="accountName" />
      <el-table-column label="分类名称" align="center" prop="categoryName" />
      <el-table-column label="收入金额" align="center" prop="amount">
        <template slot-scope="scope">
          <span class="income-amount">¥{{ scope.row.amount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收入日期" align="center" prop="incomeDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.incomeDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="付款方" align="center" prop="payer" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === '0'" type="warning">待确认</el-tag>
          <el-tag v-else-if="scope.row.status === '1'" type="success">已确认</el-tag>
          <el-tag v-else-if="scope.row.status === '2'" type="danger">已取消</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:finance:income:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:finance:income:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改收入记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="账户" prop="accountId">
          <el-select v-model="form.accountId" placeholder="请选择账户">
            <el-option
              v-for="account in accountOptions"
              :key="account.accountId"
              :label="account.accountName"
              :value="account.accountId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="分类" prop="categoryId">
          <el-select v-model="form.categoryId" placeholder="请选择分类">
            <el-option
              v-for="category in categoryOptions"
              :key="category.categoryId"
              :label="category.categoryName"
              :value="category.categoryId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="收入金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入收入金额" type="number" />
        </el-form-item>
        <el-form-item label="收入日期" prop="incomeDate">
          <el-date-picker
            v-model="form.incomeDate"
            type="date"
            placeholder="选择收入日期"
            style="width: 100%;"
          />
        </el-form-item>
        <el-form-item label="付款方" prop="payer">
          <el-input v-model="form.payer" placeholder="请输入付款方" />
        </el-form-item>
        <el-form-item label="收入描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入收入描述" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option label="待确认" value="0" />
            <el-option label="已确认" value="1" />
            <el-option label="已取消" value="2" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listIncome, getIncome, delIncome, addIncome, updateIncome } from "@/api/finance/income";
import { listAccount } from "@/api/finance/account";

export default {
  name: "FinanceIncome",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 收入记录表格数据
      incomeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        incomeNo: null,
        accountId: null,
        categoryId: null,
        status: null,
        incomeDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        accountId: [
          { required: true, message: "账户不能为空", trigger: "change" }
        ],
        categoryId: [
          { required: true, message: "分类不能为空", trigger: "change" }
        ],
        amount: [
          { required: true, message: "收入金额不能为空", trigger: "blur" }
        ],
        incomeDate: [
          { required: true, message: "收入日期不能为空", trigger: "change" }
        ]
      },
      // 账户选项
      accountOptions: [],
      // 分类选项
      categoryOptions: [
        { categoryId: 1, categoryName: "主营业务收入" },
        { categoryId: 2, categoryName: "其他业务收入" },
        { categoryId: 3, categoryName: "投资收益" },
        { categoryId: 4, categoryName: "营业外收入" }
      ]
    };
  },
  created() {
    this.getList();
    this.getAccountOptions();
  },
  methods: {
    /** 查询收入记录列表 */
    getList() {
      this.loading = true;
      listIncome(this.queryParams).then(response => {
        this.incomeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 获取账户选项 */
    getAccountOptions() {
      listAccount({}).then(response => {
        this.accountOptions = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        incomeId: null,
        incomeNo: null,
        accountId: null,
        categoryId: null,
        amount: null,
        incomeDate: null,
        payer: null,
        description: null,
        attachmentUrl: null,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.incomeId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加收入记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const incomeId = row.incomeId || this.ids
      getIncome(incomeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改收入记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.incomeId != null) {
            updateIncome(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addIncome(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const incomeIds = row.incomeId || this.ids;
      this.$modal.confirm('是否确认删除收入记录编号为"' + incomeIds + '"的数据项？').then(function() {
        return delIncome(incomeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/finance/income/export', {
        ...this.queryParams
      }, `income_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
.income-amount {
  color: #67c23a;
  font-weight: 500;
}
</style> 